package za.co.sbi.sbiexam.service;

import java.sql.ResultSet;
import java.sql.SQLException;

public class User {

	public static final int GUEST = 0;
	public static final int CANDIDATE = 1;
	public static final int ADMINISTRATOR = 2;
	public static final int INVIGILATOR = 3;

	public static final String EXAMKEY = "examkey";
	public static final String USERPASS = "userpass";

	private long userId = 0;
	private long accessLevelId = GUEST;
	private String username = null;
	private String password = null;
	private String firstname = null;
	private String lastname = null;
	private String email = null;
	private String identityNo = null;

	private User(long userId, String username, String password, int accessLevelId, String firstname, String lastname, String email, String identityNo) {
		this.userId = userId;
		this.username = username;
		this.password = password;
		this.accessLevelId = accessLevelId;
		this.firstname = firstname;
		this.lastname = lastname;
		this.email = email;
		this.identityNo = identityNo;
	}
	
	public static User loadUser(long userId) throws ClassNotFoundException, SQLException {
		ResultSet rs = null;
		User user = null;
		try {
			rs = DatabaseHelper.select("select * from user where `id` = " + userId);
			while (rs.next()) {
				user = new User (userId, rs.getString("username"), rs.getString("password"), rs.getInt("accessLevelId"), rs.getString("firstname"), rs.getString("lastname"), rs.getString("email"), rs.getString("identityNo"));
			}
		} catch (SQLException sqlex) {
			if (rs != null) rs.close();
			throw sqlex;
		}
		return user;
	}

	public long getAccessLevelId() {
		return accessLevelId;
	}

	public String getEmail() {
		return email;
	}

	public String getFirstname() {
		return firstname;
	}

	public String getIdentityNo() {
		return identityNo;
	}

	public String getLastname() {
		return lastname;
	}

	public String getPassword() {
		return password;
	}

	public long getUserId() {
		return userId;
	}

	public String getUsername() {
		return username;
	}

}
